<?php

namespace app\screen\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;

class Work extends ApiBase
{
    //获取零活列表
    public function publicList(){
        $order = input("order", 0, "intval");
        $type = input("type", 0, "intval");
        $page = input("page", 0, "intval");
        $limit = input("limit", 0, "intval");
        $name = $this->request->param("name", "", "trim");
        $longitude = $this->request->param("longitude", "", "trim");
        $latitude = $this->request->param("latitude", "", "trim");
        //筛选
        $valuation = input("valuation", 0, "intval");
        $minPrice = $this->request->param("minPrice", "", "trim");
        $maxPrice = $this->request->param("maxPrice", "", "trim");
        $minStartTime = $this->request->param("minStartTime", "", "trim");
        $maxStartTime = $this->request->param("maxStartTime", "", "trim");
        $minPopulation = $this->request->param("minPopulation", "", "trim");
        $maxPopulation = $this->request->param("maxPopulation", "", "trim");

        //筛选条件
        $where = [
            ['name','like','%'.trim($name).'%'],
            ['is_show','=',1], //公开
            //['process_status','=',1], //招工中
            //['start_time','>',time()] //开工日期大于当前时间
        ];
        $type != 0 && $where[] = ['type','=',$type];
        $valuation != 0 && $where[] = ['valuation','=',$valuation];
        $minPrice != '' && $where[] = ['price','>=',$minPrice];
        $maxPrice != '' && $where[] = ['price','<=',$maxPrice];
        $minStartTime != '' && $where[] = ['start_time','>=',strtotime($minStartTime.' 00:00:00')];
        $maxStartTime != '' && $where[] = ['start_time','<=',strtotime($maxStartTime.' 23:59:59')];
        $minPopulation != '' && $where[] = ['population','>=',$minPopulation];
        $maxPopulation != '' && $where[] = ['population','<=',$maxPopulation];

        $workBis = new \app\common\business\Work();
        $list = $workBis->getListByWhere($where,0,0,'id,department_id,name,longitude,latitude,address,start_time,price,valuation,show_time,process_status,user_id,duration');
        $total=$workBis->getCountByWhere($where,'id');
        $data = [
            'longitude'=>$longitude,
            'latitude'=>$latitude
        ];
        if (!empty($list)) {
            $list = $workBis->handlerListByOrder($list,$order,$data);//根据order返回不同list

            //数组分页
            $start = ($page - 1) * $limit;
            $list = array_values($list);
            $list = array_slice($list,$start,$limit);
            //reset
            $list = $workBis->resetTextList($list);
        }
        return Show::success(['list'=>$list,'count'=>$total], "OK");
    }

    //获取零活详情
    public function publicDetail()
    {
        $id = input("id", 0, "intval");

        $workBis = new \app\common\business\Work();
        //筛选条件
        $where = [
            ['id','=',$id],
            ['is_show','=',1], //公开
            //['process_status','=',1], //招工中
            //['start_time','>',time()] //开工日期大于当前时间 若增加此条件则无法查看开工后的详情
        ];
        $info = $workBis->getNormalByWhere($where);
        $userInfo = cache(config("redis.token_pre") . $this->request->header("token"));
        if (empty($info)) return Show::error([],'数据不存在');
        $info = $workBis->resetTextList([$info], $userInfo['id']);

        return Show::success($info[0], "OK");
    }
}
